package xm.com.handlertest;

import android.os.Build;
import android.os.Looper;
import android.support.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.concurrent.LinkedTransferQueue;


public class TestThreadMsg2 {


    static class Thread1 extends Thread{
        //线程的阻塞消息队列
        LinkedTransferQueue mQueue = new LinkedTransferQueue();
        @Override
        public void run() {
            super.run();
            for (;;) {
                String message = null;
                try {
                    message = (String) mQueue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程:"+Thread.currentThread().getId()+"  收到:"+message);
                if (message.equals("子线程去干活吧！")){
                    System.out.println("子线程:"+Thread.currentThread().getId()+"  do work!");
                    System.out.println("");
                    System.out.println("");
                }
            }
        }
    }


    public static void main(String[] args){
        Thread1 t1 = new Thread1();
        t1.start();

        for (;;) {
            try {
                String message="子线程去干活吧！";
                System.out.println("主线程:"+Thread.currentThread().getId()+"  发送:"+message);
                t1.mQueue.put(message);
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
